package com.dtjd.playcoinmonkey.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.databinding.ActivityPublishBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;

import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class PublishActivity extends AppCompatActivity {
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityPublishBinding binding = ActivityPublishBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.publishCancel.setOnClickListener(view -> finish());
        binding.publishAccept.setOnClickListener(view -> {
            List<HttpParaBean> para=new ArrayList<>();
            para.add(new HttpParaBean("token","token"));
            Network.post(para, "wbh/verifyToken", new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    e.printStackTrace();
                }
                @RequiresApi(api = Build.VERSION_CODES.R)
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) {
                    runOnUiThread(() -> {
                        try {
                            JSONObject jsonObject = new JSONObject(Objects.requireNonNull(response.body()).string());
                            if (jsonObject.getInt("code")==2){
                                if (Tools.hasExternalStoragePermission(PublishActivity.this)){
                                    startActivity(new Intent(PublishActivity.this,PostActivity.class));
                                    finish();
                                }else {
                                    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,Manifest.permission.MANAGE_EXTERNAL_STORAGE}, 0);
                                }

                            }else {
                                Tools.toast(jsonObject.getString("datalist"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });

                }
            });
        });
    }
}